﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using IUAP.DAL.Articulo;

/// <summary>
/// Descripción breve de ArticlesProvider
/// </summary>
namespace IUAP.DAL.Provider
{
    public abstract class ArticuloProvider : DataAccess
    {
        static private ArticuloProvider _instance = null;
        static public ArticuloProvider Instance
        {
            get
            {
                if (_instance == null)
                    _instance = (ArticuloProvider)Activator.CreateInstance(
                       Type.GetType(Globals.Settings.Articulo.ProviderType));
                return _instance;
            }
        }
        public ArticuloProvider()
        {
            this.ConnectionString = Globals.Settings.Articulo.ConnectionString;
            this.EnableCaching = Globals.Settings.Articulo.EnableCaching;
            this.CacheDuration = Globals.Settings.Articulo.CacheDuration;
        }
        // metodos que trabajaran con las categorias
        public abstract List<CategoriaDetalles> GetCategoria();
        public abstract CategoriaDetalles GetCategoriabyID(int id_categoria);
        public abstract bool DeleteCategoria(int id_categoria);
        public abstract bool UpdateCategoria(CategoriaDetalles categoria);
        public abstract int InsertCategoria(CategoriaDetalles categoria);

        // metodos que trabajaran con los articulos
        public abstract List<ArticuloDetalles> GetArticulo(int pageIndex, int pageSize);
        public abstract List<ArticuloDetalles> GetArticulobyCategoria(int id_categoria, int pageIndex, int pageSize);
        public abstract int GetArticleCount();
        public abstract int GetContarArticulosbyCategoria(int id_categoria);
        public abstract List<ArticuloDetalles> GetArticuloPublicado(DateTime fecha_actual, int pageIndex, int pageSize);
        public abstract List<ArticuloDetalles> GetArticuloPublicadobyCategoria(int id_categoria, DateTime fecha_actual, int pageIndex, int pageSize);
        public abstract int GetContarArticulosPublicados(DateTime fecha_actual);
        public abstract int GetContarArticulosPublicadosbyCategoria(int id_categoria, DateTime fecha_actual);
        public abstract ArticuloDetalles GetArticulobyID(int id_articulo);
        public abstract bool DeleteArticulo(int id_articulo);
        public abstract bool UpdateArticulo(ArticuloDetalles articulo);
        public abstract int InsertArticulo(ArticuloDetalles articulo);
        public abstract bool AprobarArticulo(int id_articulo);
        public abstract bool IncrementCantidadVisitas(int id_articulo);
        public abstract bool InsertVoto(int articleID, int rank_articulo);
        public abstract string GetArticuloCont(int id_articulo);

        // metodos que trabajaran con los comentarios
        public abstract List<ComentarioDetalles> GetComentario(int pageIndex, int pageSize);
        public abstract List<ComentarioDetalles> GetComentariobyArticulo(int id_articulo, int pageIndex, int pageSize);
        public abstract int GetContarComentario();
        public abstract int GetContarComentariobyArticulo(int articleID);
        public abstract ComentarioDetalles GetComentariobyID(int commentID);
        public abstract bool DeleteComentario(int commentID);
        public abstract bool UpdateComentario(ComentarioDetalles articulo);
        public abstract int InsertComentario(ComentarioDetalles articulo);

        protected virtual CategoriaDetalles GetCategoryFromReader(IDataReader reader)
        {
            return new CategoriaDetalles(
               (int)reader["id_categoria"],
               (DateTime)reader["fech_cate"],
               reader["usua_cate"].ToString(),
               reader["titu_cate"].ToString(),
               (int)reader["impo_cate"],
               reader["desc_cate"].ToString(),
               reader["imgurl_cate"].ToString());
        }
        protected virtual List<CategoriaDetalles> GetCategoryCollectionFromReader(IDataReader reader)
        {
            List<CategoriaDetalles> categories = new List<CategoriaDetalles>();
            while (reader.Read())
                categories.Add(GetCategoryFromReader(reader));
            return categories;
        }
        protected virtual ArticuloDetalles GetArticleFromReader(IDataReader reader)
        {
            return GetArticleFromReader(reader, true);
        }
        protected virtual ArticuloDetalles GetArticleFromReader(IDataReader reader, bool readBody)
        {
            ArticuloDetalles articulo = new ArticuloDetalles(
               (int)reader["id_articulo"],
               (DateTime)reader["fech_arti"],
               reader["usua_arti"].ToString(),
               (int)reader["id_categoria"],
               reader["titu_arti"].ToString(),
               reader["resu_arti"].ToString(),
               null,
               reader["ciud_arti"].ToString(),
               (reader["fech_publ_arti"] == DBNull.Value ? DateTime.Now : (DateTime)reader["fech_publ_arti"]),
               (reader["fech_expi_arti"] == DBNull.Value ? DateTime.MaxValue : (DateTime)reader["fech_expi_arti"]),
               (bool)reader["apro_arti"],
               (bool)reader["list_arti"],
               (bool)reader["actv_comn_arti"],
               (bool)reader["solo_miem_arti"],
               (int)reader["cont_visi_arti"],
               (int)reader["voto_arti"],
               (int)reader["rank_arti"]);

            if (readBody)
                articulo.Cont_arti = reader["cont_arti"].ToString();

            return articulo;
        }
        protected virtual List<ArticuloDetalles> GetArticleCollectionFromReader(IDataReader reader)
        {
            return GetArticleCollectionFromReader(reader, true);
        }
        protected virtual List<ArticuloDetalles> GetArticleCollectionFromReader(IDataReader reader, bool readBody)
        {
            List<ArticuloDetalles> articulos = new List<ArticuloDetalles>();
            while (reader.Read())
                articulos.Add(GetArticleFromReader(reader, readBody));
            return articulos;
        }
        protected virtual ComentarioDetalles GetCommentFromReader(IDataReader reader)
        {
            return new ComentarioDetalles(
               (int)reader["id_comentario"],
               (DateTime)reader["fech_come"],
               reader["usua_come"].ToString(),
               reader["mail_usua_come"].ToString(),
               reader["ip_usua_come"].ToString(),
               (int)reader["id_articulo"],
               reader["cont_come"].ToString());
        }
        protected virtual List<ComentarioDetalles> GetCommentCollectionFromReader(IDataReader reader)
        {
            List<ComentarioDetalles> comentarios = new List<ComentarioDetalles>();
            while (reader.Read())
                comentarios.Add(GetCommentFromReader(reader));
            return comentarios;
        }
    }
}